// pages/joinUs/joinUsIndex/joinUsIndex.js
var initForm = require('./module/initForm');
var submitForm = require('./module/submitForm');
var resetForm = require('./module/resetForm');
var filesM = wx.getFileSystemManager();
//输入表单号查看的结果
var checkForm = [];
//输入表单号查看的表单ID
var checkFormId = '';
//页面状态码，0为转发，1为自己登录
var pageStatus = '';
var app = getApp();
//页面要显示的表单
var showForm = [];
Page({
    /**
     *自定义函数 
     */

    //页面初始化，获取表单数据
    pageInit: function() {
        var that = this;
        if (pageStatus == 1) {
            //新的表单
            if (app.globalData.user_form[0].fill_state == 'new') {
                //如果是新建表单则允许填写
                this.setData({
                    disabled: false
                });
                showForm = app.globalData.user_form;
                //赋值后将全局变量清空
                app.globalData.user_form = [];
                //改变标题栏名称
                switch (showForm[0].form_org) {
                    case '02':
                        wx.setNavigationBarTitle({
                            title: '加入西电官微',
                        })
                        break;
                    case '03':
                        break;
                    case '04':
                        break;
                }
                //给页面赋值
                this.setData({
                    form_num: showForm
                });
                //显示表格
                this.setData({
                    show_form: true
                });
                //显示提示
                this.setData({
                    state: 'new'
                });
                //获取用户信息
                wx.getUserInfo({
                    success(res) {
                        var info = res.userInfo;
                        //给新的全局变量赋值保存用户的回答
                        initForm(info, showForm);
                        //设置页面用户信息变量
                        that.setData({
                            form_id: app.globalData.user_form[0].fill_id
                        });
                        that.setData({
                            form_time: app.globalData.user_form[0].fill_time.time
                        });
                        that.setData({
                            user_nickName: app.globalData.user_form[0].fill_nickName
                        });
                    }
                })
            } else if (app.globalData.user_form[0].fill_state == 'old') {
                //如果是旧表单则不允许填写
                this.setData({
                    disabled: true
                });
                //旧的表单
                showForm = app.globalData.user_form;
                //赋值后将全局变量清空
                app.globalData.user_form = [];
                //改变标题栏名称
                wx.setNavigationBarTitle({
                    title: '查看我的招新表',
                });
                //给页面赋值
                this.setData({
                    form_num: showForm
                });
                //先显示输入表单id页面
                this.setData({
                    state: 'input_form_id'
                });
                //暂时不显示表单
                this.setData({
                    show_form: false
                });
            }
        } else if (pageStatus == 0) {
            //如果是新建表单则允许填写
            this.setData({
                disabled: false
            });
            //赋值后将全局变量清空
            app.globalData.user_form = [];
            //改变标题栏名称
            switch (showForm[0].form_org) {
                case '02':
                    wx.setNavigationBarTitle({
                        title: '加入西电官微',
                    })
                    break;
                case '03':
                    break;
                case '04':
                    break;
            }
            //给页面赋值
            this.setData({
                form_num: showForm
            });
            //显示表格
            this.setData({
                show_form: true
            });
            //显示提示
            this.setData({
                state: 'new'
            });
            //获取用户信息
            wx.getUserInfo({
                success(res) {
                    var info = res.userInfo;
                    //给新的全局变量赋值保存用户的回答
                    initForm(info, showForm);
                    //设置页面用户信息变量
                    that.setData({
                        form_id: app.globalData.user_form[0].fill_id
                    });
                    that.setData({
                        form_time: app.globalData.user_form[0].fill_time.time
                    });
                    that.setData({
                        user_nickName: app.globalData.user_form[0].fill_nickName
                    });
                }
            })
        }
    },

    /**
     * 页面函数
     */

    //输入表单号
    input_form_id_bind: function(e) {
        checkFormId = e.detail.value;
    },

    //点击确定按钮
    bind_i_button_input_form_id: function() {
        var that = this;
        wx.request({
            url: `https://wx.jingqi.ink/wxapp/form/get_publish_data/${showForm[0].form_id}/${checkFormId}`,
            method: 'GET',
            success(res) {
                if (res.data[0] == undefined) {
                    wx.showModal({
                        title: '提示',
                        content: '没有这个暗号，请重新输入',
                        showCancel: false,
                        confirmText: '我知道了'
                    })
                } else {
                    //新建数组
                    for (var prop in res.data[0]) {
                        for (var i = 0; i < showForm.length; i++) {
                            if (showForm[i].item_name == prop) {
                                checkForm[checkForm.length] = {};
                                checkForm[checkForm.length - 1].form_q = prop;
                                checkForm[checkForm.length - 1].form_a = res.data[0][prop];
                            }
                        }
                    }
                    //页面赋值
                    that.setData({
                        form_check: checkForm
                    });
                    //更改显示页面
                    that.setData({
                        state: 'check_form'
                    });
                }
            }
        })
    },

    //单行文本框输入
    text_field_input: function(e) {
        var res = e.detail.detail.value;
        var id = e.currentTarget.id;
        if (app.globalData.user_form[id].fill_a == undefined) {
            app.globalData.user_form[id].fill_a = '';
        }
        app.globalData.user_form[id].fill_a = res;
    },

    //多行文本框输入
    text_fields_input: function(e) {
        var res = e.detail.detail.value;
        var id = e.currentTarget.id;
        if (app.globalData.user_form[id].fill_a == undefined) {
            app.globalData.user_form[id].fill_a = '';
        }
        app.globalData.user_form[id].fill_a = res;
    },

    //单项选择输入
    select_input: function(e) {
        var res = e.detail.value;
        var id = e.currentTarget.id;
        //第一次选择新建一项
        if (showForm[id].options_res == undefined) {
            showForm[id].options_res = '';
        }
        if (showForm[id].options_res == res) { //如果原来有则取消选择
            showForm[id].options_res = '';
        } else if (showForm[id].options_res != res) { //如果原来没有则选择上
            showForm[id].options_res = res;
        }
        this.setData({
            form_num: showForm
        });
        if (app.globalData.user_form[id].fill_a == undefined) {
            app.globalData.user_form[id].fill_a = '';
        }
        app.globalData.user_form[id].fill_a = showForm[id].options_res;
    },

    //多项选择输入
    selects_input: function(e) {
        var res = e.detail.value;
        var id = e.currentTarget.id;
        //第一次选择新建一项
        if (showForm[id].options_res == undefined) {
            showForm[id].options_res = new Array(showForm[id].options.length);
        }
        //在数组里查找刚才选中的项目
        var temp = showForm[id].options_res.indexOf(res);
        if (temp == -1) { //如果数组里没有则插入数组
            showForm[id].options_res[showForm[id].options.indexOf(res)] = res;
        } else { //如果数组里有则删除这一项
            showForm[id].options_res[showForm[id].options.indexOf(res)] = '';
        }
        //赋值给页面变量
        this.setData({
            form_num: showForm
        });
        var j = 0;
        if (app.globalData.user_form[id].fill_a == undefined) {
            app.globalData.user_form[id].fill_a = '';
        }
        app.globalData.user_form[id].fill_a = []
        for (var i = 0; i < showForm[id].options_res.length; i++) {
            if (showForm[id].options_res[i] != undefined && showForm[id].options_res[i] != '') {
                app.globalData.user_form[id].fill_a[j] = showForm[id].options_res[i];
                j++;
            }
        }
    },

    //数字文本框输入
    number_input: function(e) {
        var res = e.detail.detail.value;
        var id = e.currentTarget.id;
        if (app.globalData.user_form[id].fill_a == undefined) {
            app.globalData.user_form[id].fill_a = '';
        }
        app.globalData.user_form[id].fill_a = res;
    },

    //附件输入
    files_input: function(e) {
        filesM.readFile({
            filePath: '/',
            success(res) {},
            complete(res) {}
        });
    },

    //提交报名表
    bind_i_button_submit_form: function() {
        submitForm();
    },

    //重置报名表
    bind_i_button_reset_form: function() {
        var that = this;
        wx.showModal({
            title: '警告',
            content: '这将重置整个表单，您将失去所有已填写的内容，是否继续？',
            confirmColor: '#ff0000',
            confirmText: '确定重置',
            cancelText: '返回',
            success(res) {
                if (res.confirm) {
                    showForm = resetForm(showForm);
                    that.setData({
                        form_num: showForm
                    });
                    that.setData({
                        text_field_value: ''
                    });
                    that.setData({
                        text_fields_value: ''
                    });
                    that.setData({
                        number_value: ''
                    });
                }
            }
        })
    },

    /**
     * 页面的初始数据
     */
    data: {
        //新表还是旧表
        state: '',
        //是否显示表单
        show_form: '',
        //默认不允许填写
        disabled: true
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //设置页面状态码
        if ('temp' in options) { //转发的页面
            pageStatus = 0;
            pageForm = JSON.parse(options.temp);
        } else { //自己登录的页面
            pageStatus = 1;
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        //页面初始化，获取表单数据
        this.pageInit();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        var title = '';
        switch (showForm[0].form_org) {
            case '02':
                title = '加入西电官微';
                break;
            case '03':
                break;
            case '04':
                break;
        }
        return {
            title: title,
            path: 'pages/joinUs/joinUsIndex/joinUsIndex?temp=' + JSON.stringify(showForm),
        }
    }
})